package com.ug_project.alert

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.graphics.Color
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.Window
import android.widget.LinearLayout
import android.widget.RelativeLayout
import com.ug_project.views.R
import com.ug_project.views.findLinearLayout
import com.ug_project.views.findTextView
import com.ug_project.views.meaSuredHeight
import com.ug_project.window.DecorView

/**
 * Created by yeganaaa on 3/25/2017.
 */
abstract class Alerter( window: Window) : DecorView(window) {
    val rootView: RelativeLayout by lazy {
        LayoutInflater.from(window.context).inflate(R.layout.layout_alerter, null) as RelativeLayout
    }

    val ButtonPanel: LinearLayout by lazy {
        rootView.findLinearLayout(R.id.ButtonPanel)
    }

    val containerView: LinearLayout by lazy {
        rootView.findLinearLayout(R.id.containerView)
    }

    var title: String = ""
        get() = field
        set(value) {
            field = value
            rootView.findTextView(R.id.Title).text = value
        }

    var content: String = ""
        get() = field
        set(value) {
            field = value
            rootView.findTextView(R.id.Content).text = value
        }

    var animationDuration: Long = 500
    get() = field
    set(value) {
        field = value
    }

    var isClosed = false

    var closedEvent: () -> Unit = {}

    var backColor: Int = Color.RED
    get() = field
    set(value) {
        field = value
        containerView.setBackgroundColor(value)
    }

    var foreColor: Int = Color.WHITE
        get() = field
        set(value) {
            field = value
            rootView.findTextView(R.id.Title).setTextColor(value)
            rootView.findTextView(R.id.Content).setTextColor(value)
        }

    open fun showAnimation(): AnimatorSet {
        val sv = containerView.meaSuredHeight.toFloat()
        val animator = ObjectAnimator.ofFloat(rootView, "translationY", (-sv * 2.2).toFloat(), 0f)
        val animator1 = ObjectAnimator.ofFloat(rootView, "rotationX", 90f, 0f)
        val set = AnimatorSet()
        set.play(animator).with(animator1)
        set.duration = animationDuration
        return set
    }

    abstract fun onShow()

    open fun show() {
        showAnimation().start()
        DecorView.addView(rootView)

        onShow()
    }

    open fun closeAnimation(): AnimatorSet {
        val set = AnimatorSet()

        val sv = containerView.meaSuredHeight.toFloat()
        val animator = ObjectAnimator.ofFloat(rootView, "translationY", 0f, -sv)

        set.play(animator)
        set.duration = animationDuration

        return set
    }

    open fun close() {
        isClosed = true
        val cla = closeAnimation()
        cla.addListener(object : Animator.AnimatorListener {
            override fun onAnimationRepeat(animation: Animator?) {

            }

            override fun onAnimationEnd(animation: Animator?) {
                DecorView.removeView(rootView)
                closedEvent()
            }

            override fun onAnimationCancel(animation: Animator?) {

            }

            override fun onAnimationStart(animation: Animator?) {

            }

        })
        cla.start()
    }
}